package com.richpencil.utils;

import java.util.*;

public class RedPackUtil {
	/**
	* 生成6位或10位随机数 param codeLength(多少位)
	* 
	* @return
	*/
	private static String createCode(int codeLength) {
		String code = "";
		for (int i = 0; i < codeLength; i++) {
			code += (int) (Math.random() * 9);
		}
		return code;
	}


	private static boolean isValidChar(char ch) {
		if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z')|| (ch >= 'a' && ch <= 'z'))
			return true;
		if ((ch >= 0x4e00 && ch <= 0x7fff) || (ch >= 0x8000 && ch <= 0x952f))
			return true;// 简体中文汉字编码
		return false;
	}


	/**
	* 除去数组中的空值和签名参数
	* 
	* @param sArray
	*            签名参数组
	* @return 去掉空值与签名参数后的新签名参数组
	*/
	public static Map<String, String> paraFilter(Map<String, String> sArray) {
	
		Map<String, String> result = new HashMap<String, String>();
		if (sArray == null || sArray.size() <= 0) {
			return result;
		}	
		for (String key : sArray.keySet())
		{
			String value = sArray.get(key);
			if (value == null || value.equals("")|| key.equalsIgnoreCase("sign")|| key.equalsIgnoreCase("sign_type")) {
				continue;
			}
			result.put(key, value);
		}
		return result;
	}


	/**
	* 把数组所有元素排序，并按照"参数=参数值"的模式用"&"字符拼接成字符串
	* 
	* @param params
	*            需要排序并参与字符拼接的参数组
	* @return 拼接后字符串
	*/
	public static String createLinkString(Map<String, String> params) {
	
		List<String> keys = new ArrayList<String>(params.keySet());
		Collections.sort(keys);
		String prestr = "";
		for (int i = 0; i < keys.size(); i++) {
			String key = keys.get(i);
			String value = params.get(key);
			
			if (i == keys.size() - 1) {// 拼接时，不包括最后一个&字符
				prestr = prestr + key + "=" + value;
			} else {
				prestr = prestr + key + "=" + value + "&";
			}
		}
		return prestr;
	}

	

}
